/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.OmSafetyEquipment;
import me.zhengjie.modules.cspimp.psp.domain.vo.OmSafetyEquipmentVo;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.OmSafetyEquipmentRepository;
import me.zhengjie.modules.cspimp.psp.service.OmSafetyEquipmentService;
import me.zhengjie.modules.cspimp.psp.service.dto.OmSafetyEquipmentDto;
import me.zhengjie.modules.cspimp.psp.service.dto.OmSafetyEquipmentQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.OmSafetyEquipmentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zmm
 * @website https://docs.auauz.net
 * @description 服务实现
 * @date 2020-05-30
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OmSafetyEquipmentServiceImpl implements OmSafetyEquipmentService {

    private final OmSafetyEquipmentRepository omSafetyEquipmentRepository;
    private final OmSafetyEquipmentMapper omSafetyEquipmentMapper;

    @Override
    public Map<String, Object> queryAll(OmSafetyEquipmentQueryCriteria criteria, Pageable pageable) {
        Page<OmSafetyEquipment> page = omSafetyEquipmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(omSafetyEquipmentMapper::toDto));
    }

    @Override
    public List<OmSafetyEquipmentDto> queryAll(OmSafetyEquipmentQueryCriteria criteria) {
        return omSafetyEquipmentMapper.toDto(omSafetyEquipmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    public OmSafetyEquipmentDto findById(Long id) {
        OmSafetyEquipment omSafetyEquipment = omSafetyEquipmentRepository.findById(id).orElseGet(OmSafetyEquipment::new);
        ValidationUtil.isNull(omSafetyEquipment.getId(), "OmSafetyEquipment", "id", id);
        return omSafetyEquipmentMapper.toDto(omSafetyEquipment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmSafetyEquipmentDto create(OmSafetyEquipmentVo resources) {
        OmSafetyEquipment omSafetyEquipment = new OmSafetyEquipment();
        BeanUtils.copyProperties(resources, omSafetyEquipment);
        omSafetyEquipment.setCid(0l);
        String buyDateStr = resources.getBuyDate();
        if (StringUtils.isNotBlank(buyDateStr)) {
//            Date buyDate = null;
//            try {
//                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//                buyDate = sf.parse(buyDateStr);
//            } catch (Exception e) {
//                e.getMessage();
            omSafetyEquipment.setBuyDate(buyDateStr);
        }
//    }

        omSafetyEquipmentRepository.save(omSafetyEquipment);
        return omSafetyEquipmentMapper.toDto(omSafetyEquipment);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OmSafetyEquipmentVo resources) {
        OmSafetyEquipment omSafetyEquipment = omSafetyEquipmentRepository.findById(resources.getId()).orElseGet(OmSafetyEquipment::new);
        ValidationUtil.isNull(omSafetyEquipment.getId(), "OmSafetyEquipment", "id", resources.getId());
        BeanUtils.copyProperties(resources, omSafetyEquipment);
        String buyDateStr = resources.getBuyDate();
        if (StringUtils.isNotBlank(buyDateStr)) {
//            Date buyDate = null;
//            try {
//                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//                buyDate = sf.parse(buyDateStr);
//            } catch (Exception e) {
//                e.getMessage();
//            }
            omSafetyEquipment.setBuyDate(buyDateStr);
        }
        omSafetyEquipmentRepository.save(omSafetyEquipment);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            omSafetyEquipmentRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OmSafetyEquipmentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OmSafetyEquipmentDto omSafetyEquipment : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("创建者", omSafetyEquipment.getCreateBy());
            map.put("更新者", omSafetyEquipment.getUpdateBy());
            map.put("创建日期", omSafetyEquipment.getCreateTime());
            map.put("更新时间", omSafetyEquipment.getUpdateTime());
            map.put("设施名称", omSafetyEquipment.getName());
            map.put("设施数量", omSafetyEquipment.getEquCount());
            map.put("备注", omSafetyEquipment.getRemark());
            map.put("复检周期", omSafetyEquipment.getRecheckCycle());
            map.put("购买日期", omSafetyEquipment.getBuyDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}